/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.demo.list;

import java.util.List;

import android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * How to customize the Adapter ?
 *  1) Extended the ArrayAdapter
 *  2) Override method :
 * getView : called when ListView need to display a new cell of the list
 * @author hong
 */
public class ContactAdapter extends ArrayAdapter<Contact> {

	/**
	 * The view ID identification in XML GUI
	 */
	private int viewId;
	
    public ContactAdapter(Context context, int textViewResourceId, List<Contact> objects) {
        super(context, textViewResourceId, objects);
        viewId = textViewResourceId;
    }

    public ContactAdapter(Context context, int textViewResourceId, Contact[] objects) {
        super(context, textViewResourceId, objects);
        viewId = textViewResourceId;   
    }    

    
    /**
     * This method is called when list view required to display a new Cell of the list.
     * TAKECARE :
     * ListView works like IPhone ListView, system will avoid unnecessary allocation of view
     * and try to recycle a view which are not see by user.
     * 
     * position    : Index of the data to print in the Data List Model
     * convertView : The recycled view if it was possible (if no, it contains NULL means you 
     * must create the view)
     * parent      : The container of the view
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /* Check if we re-used a new view */
    	View view = convertView;
        if (view == null)
        {
            /* Create a new View */
            LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = li.inflate(viewId, null);
        }

        /* Initialize the view */
        Contact c = getItem(position);
        if (c != null)
        {
            TextView name = (TextView)view.findViewById(R.id.TextView01);
            TextView firstname  = (TextView)view.findViewById(R.id.TextView02);

            name.setText(c.getName());
            firstname.setText(c.getFirstName());
        }

        return view;
    }



}
